#!/bin/bash
set -e
export LC_ALL=C
if [ $# -eq 0 ]; then
  install=false
else
  if [ "$1" = "install" ]; then
      install=true
  else
      echo "Invalid command, supported commands are: 'install'"
      exit 1
  fi
fi

PYTHON=${PYTHON:-"python3"}
VENV=${VENV:-"virtualenv"}

case "$(uname -s)" in
Linux)
    case "$(lsb_release --id --short)" in
    Ubuntu|Debian|LinuxMint)
        # Ensure setuptools is installed
        if [[ "$PYTHON" =~ "python2" ]]; then
            deps=(qemu-utils python-dev libssl-dev python-pip python-virtualenv libev-dev libvirt-dev libmysqlclient-dev libffi-dev libyaml-dev)
        else
            deps=(qemu-utils python3-dev libssl-dev python-pip python3-virtualenv libev-dev libvirt-dev libmysqlclient-dev libffi-dev libyaml-dev)
        fi
        for package in ${deps[@]}; do
            if [ "$(dpkg --status -- $package|sed -n 's/^Status: //p')" != "install ok installed" ]; then
                # add a space after old values
                missing="${missing:+$missing }$package"
            fi
        done
        if [ -n "$missing" ]; then
            echo "$0: missing required packages:" 1>&2
            echo "$missing"
            if [ "$install" = true ]; then
                echo "Installing missing packages..."
                sudo apt-get -y install $missing
            else
                echo "Please install missing packages or run './bootstrap install' if you have sudo"
                echo "sudo apt-get -y install $missing"
                exit 1
            fi
        fi
        ;;
    RedHatEnterpriseWorkstation|RedHatEnterpriseServer|CentOS)
        for package in python2-pip python-virtualenv mariadb-devel libev-devel libvirt-devel libffi-devel; do
	    if [ "$(rpm -q $package)" == "package $package is not installed" ]; then
		missing="${missing:+$missing }$package"
	    fi
        done
	if [ -n "$missing" ]; then
            echo "$0: missing required packages:" 1>&2
            echo "$missing"
            if [ "$install" = true ]; then
                echo "Installing missing packages..."
                sudo yum -y install $missing
            else
                echo "Please install missing packages or run './bootstrap install' if you have sudo"
                echo "sudo yum -y install $missing"
                exit 1
            fi
	fi
	;;
    Fedora)
        for package in python2-pip python2-virtualenv libev-devel libvirt-devel community-mysql-devel libffi-devel; do
	    if [ "$(rpm -q $package)" == "package $package is not installed" ]; then
		missing="${missing:+$missing }$package"
	    fi
	done
	fedora_release=$(lsb_release -rs)
	package_manager=dnf
	if [ $fedora_release -lt 23 ]; then
	    package_manager=yum
	fi
	if [ -n "$missing" ]; then
            echo "$0: missing required packages:" 1>&2
            echo "$missing"
            if [ "$install" = true ]; then
                echo "Installing missing packages..."
                sudo $package_manager -y install $missing
            else
                echo "Please install missing packages or run './bootstrap install' if you have sudo"
                echo "sudo $package_manager -y install $missing"
                exit 1
            fi
	fi
	;;
    "openSUSE project"|"SUSE LINUX"|"openSUSE")
        for package in python-pip python-devel python-virtualenv libev-devel libvirt-devel libmysqlclient-devel libffi-devel; do
            if [ "$(rpm -q $package)" == "package $package is not installed" ]; then
                if [ "$(rpm -q --whatprovides $package)" == "no package provides $package" ]; then
                    missing="${missing:+$missing }$package"
                fi
            fi
        done
        if [ -n "$missing" ]; then
            echo "$0: missing required packages, please install them:" 1>&2
            echo "sudo zypper install $missing"
            exit 1
        fi
        ;;
    *)
        echo "This script does not support your Linux distribution yet. Patches encouraged!"
        exit 1
        ;;
    esac
    ;;
Darwin)
    if ! which brew > /dev/null; then
        echo "You need Homebrew: http://brew.sh/"
        exit 1
    fi
    for keg in python libvirt libev mysql libffi; do
        if brew list $keg >/dev/null 2>&1; then
            echo "Found $keg"
        else
            brew install $keg
        fi
    done
    ;;
*)
    echo "This script does not support your OS yet. Patches encouraged!"
    exit 1
    ;;
esac

# Forcibly remove old virtualenvs which used system site-packages
if [ -e ./$VENV ]  && [ ! -e ./$VENV/lib/python*/no-global-site-packages.txt ]; then
    echo "Removing old virtualenv because it uses system site-packages"
    rm -rf ./$VENV
fi

if [ -z "$NO_CLOBBER" ] || [ ! -e ./$VENV ]; then
    if ! which virtualenv > /dev/null; then
        pip install virtualenv
    fi
    virtualenv --python=$PYTHON --setuptools $VENV
fi

PVER=$(./$VENV/bin/python --version 2>&1 | grep Python)
LVER=${PVER#* }
SVER=${LVER%%.*}

# Upgrade pip first
./$VENV/bin/pip install --upgrade pip

# Ensure setuptools is installed
if [ "$SVER" = "2" ]; then
    ./$VENV/bin/pip install setuptools==44
else
    ./$VENV/bin/pip install setuptools --upgrade
fi

# Install all requirements
./$VENV/bin/pip install --upgrade -r requirements${SVER}.txt

# Check to make sure requirements are met
./$VENV/bin/pip check

# Remove leftover .pyc files
find teuthology -name '*.pyc' -exec rm {} \;

# Install teuthology
./$VENV/bin/python setup.py develop
